/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package dale2507.gates.gate;

import java.util.Arrays;

/**
 * Address used for dialling gates
 * @author Dale2507
 */
public class Address {
    
    /**
     * Array of address items in specific order
     */
    private AddressItem[] address = new AddressItem[9];

    /**
     * Creates a new empty address
     */
    public Address() {
    }

    /**
     * Creates an address from an address item array
     * @param address 
     */
    public Address(AddressItem[] address) {
        this.address = address;
    }

    /**
     * Get address item array
     * @return Address item array
     */
    public AddressItem[] getAddress() {
        return address;
    }
    
    /**
     * Get single address item
     * @param pos Position of address item to retrieve
     * @return Address item at specified position
     */
    public AddressItem getAddressItem(int pos) {
        return address[pos];
    }

    /**
     * Sets an address item
     * @param pos Position of item to set
     * @param address Address item to set in position specified
     */
    public void setAddress(int pos, AddressItem address) {
        this.address[pos] = address;
    }
    
    /**
     * Gets the number of items in the address. Blank spaces are not counted (e.g. x_xx_x___ would be 4 items)
     * @return Number of items in the address
     */
    public int getItemCount() {
        int count = 0;
        for (AddressItem i : this.address) {
            if (i != null) count++;
        }
        return count;
    }
    
    /**
     * Checks if the address is empty.
     * @return True if address contains no items
     */
    public boolean isEmpty() {
        if (this.getItemCount() == 0) return true;
        return false;
    }

    @Override
    public boolean equals(Object obj) {
        if (obj == null) {
            return false;
        }
        if (getClass() != obj.getClass()) {
            return false;
        }
        final Address other = (Address) obj;
        if (Arrays.deepEquals(this.address, other.address)) {
            return true;
        }
        return false;
    }

    @Override
    public int hashCode() {
        int hash = 7;
        hash = 59 * hash + Arrays.deepHashCode(this.address);
        return hash;
    }

    @Override
    public String toString() {
        String output = "";
        for (int x=0; x<address.length; x++) {
            output += "Chevron " + (x + 1) + ": " + address[x] + "\r\n";
        }
        return output;
    }
    
}
